package com.meta.act.app.tenant.page;

import java.util.List;

public class Page<T> {

    public static final int DEFAULT_PAGE_SIZE = 20;

    //页码
    protected int pageNo = 1;
    //每一页数据条数
    protected int pageSize = DEFAULT_PAGE_SIZE;
    //总记录数
    protected long totalRecords = -1;
    //总页数
    protected int totalPages = -1; 

    protected List<T> list;

    public int getPageNo() {
        return pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
        computeTotalPage();
    }

    public long getTotalRecords() {
        return totalRecords;
    }

    public int getTotalPages() {
        return totalPages;
    }

    public void setTotalRecords(long totalRecords) {
        this.totalRecords = totalRecords;
        computeTotalPage();
    }

    protected void computeTotalPage() {
        if (getPageSize() > 0 && getTotalRecords() > -1) {
            this.totalPages = (int) (getTotalRecords() % getPageSize() == 0 ? getTotalRecords() / getPageSize() : getTotalRecords() / getPageSize() + 1);
        }
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder().append("Page [pageNo=").append(pageNo).append(", pageSize=").append(pageSize)
                .append(", totalRecord=").append(totalRecords < 0 ? "null" : totalRecords).append(", totalPage=")
                .append(totalPages < 0 ? "null" : totalPages).append(", curPageObjects=").append(list == null ? "null" : list.size()).append("]");
        return builder.toString();
    }

}
